package com.example.linyiquan.app.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.example.linyiquan.app.R;
import com.example.linyiquan.app.util.MyVolley;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by linyiquan on 17-4-14.
 */

public class LoginActivity extends Activity implements View.OnClickListener {

    private EditText et_username;
    private EditText et_password;
    private TextView tv_register;
    private CheckBox cb_register;
    private Button bt_login;

    private String username;
    private String password;

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor spe;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        sharedPreferences = LoginActivity.this.getSharedPreferences("check", MODE_PRIVATE);
        init();
    }

    /**
     * 添加视图
     */
    private void init() {
        et_username = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);
        tv_register = (TextView) findViewById(R.id.tv_register);
        tv_register.setOnClickListener(this);
        bt_login = (Button) findViewById(R.id.bt_login);
        bt_login.setOnClickListener(this);
        cb_register = (CheckBox) findViewById(R.id.cb_register);
        if (sharedPreferences.getBoolean("check", false)) {
            et_username.setText(sharedPreferences.getString("username", username));
            et_password.setText(sharedPreferences.getString("password", password));
            cb_register.setChecked(true);
        }

    }

    /**
     * 添加登陆,注册的跳转以及对记住密码功能的判断进行存储
     *
     * @param v
     */
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.bt_login:
                username = et_username.getText().toString();
                password = et_password.getText().toString();

                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                    Toast.makeText(this, "用户名帐号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                boolean checkbox = cb_register.isChecked();

                if (checkbox) {
                    spe = sharedPreferences.edit();
                    spe.putString("username", username);
                    spe.putString("password", password);
                    spe.putBoolean("check", true);
                    spe.commit();
                } else {
                    spe = sharedPreferences.edit();
                    spe.putString("username", username);
                    spe.putString("password", null);
                    spe.putBoolean("check", false);
                    spe.commit();
                }

                LoginToServer(username, password);

                break;
            case R.id.tv_register:
                Intent register = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(register);
        }

    }

    /**
     * 用post方法进行数据提交
     *
     * @param username
     * @param password
     */
    public void LoginToServer(String username, String password) {
        MyVolley.init(this);

        String url = "http://120.76.96.34/weizhanjsp/user/login?clientType=1&mac=sdfsss&cards=true";

        url += "&username=" + username;
        url += "&password=" + password;

        MyVolley.post(url, null, new MyVolley.OnResultListener() {
            @Override
            public void done(JSONObject result) {
                int resultCode;
                try {
                    resultCode = result.getInt("resultCode");
                    if (resultCode == 0) {
                        Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                        Intent user = new Intent(LoginActivity.this, GridActivity.class);
                        startActivity(user);
                    } else {
                        Toast.makeText(LoginActivity.this, result.getString("resultString"), Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void fail(VolleyError error) {

            }
        });

    }

}
